package common.fengkai.filepost.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class PdfUtils {


       public static void main(String[] args) throws  Exception{

           byte[] bytes = Base64util.decoderFileTobytes("D:\\Download\\7.pdf");
           String s2 = Base64util.encodeBase64File("D:\\Download\\7.pdf");
           System.out.println(s2);
           File file = new File("D:\\Download\\7.pdf");
           List<Map<String, Object>> list= new ArrayList<>();
           PDDocument pdDocument = null;

           try {
               pdDocument = PDDocument.load(file);
               int pages = pdDocument.getNumberOfPages();
               List<PDSignature> signatureDictionaries = pdDocument.getSignatureDictionaries();
               String s = JSONObject.toJSONString(signatureDictionaries);
               log.info("读取到的印章数量:"+String.valueOf(signatureDictionaries.size()));

               // 读文本内容
               PDFTextStripper stripper=new PDFTextStripper();
               // 设置按顺序输出
               stripper.setSortByPosition(true);
               stripper.setStartPage(1);
               stripper.setEndPage(pages);
               String content = stripper.getText(pdDocument);
               System.out.println(content);

               FileWriter fileWriter = null;

               try {
                   fileWriter=new FileWriter("C:\\Users\\carry\\Desktop\\新建文本文档(4).txt");
                   fileWriter.write(s2);
               } catch(Exception e) {
                   e.printStackTrace();
               }finally {
                   fileWriter.flush();
                   fileWriter.close();
               }

           } catch(Exception e) {
               e.printStackTrace();
           }


       }


}
